﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ClassWaterSystemTypeManage : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Autodesk.Revit.DB.View activeView = document.ActiveView;
				Selection selection = activeUIDocument.Selection;
				transaction = new Transaction(document, "系统类型管理");
				transaction.Start();
				string type = "空调水系统";
				List<PipingSystemType> listPipingSystemType = this.Get_PipingSystemType(document);
				List<MechanicalSystemType> listMechanicalSystemType = this.Get_MechanicalSystemType(document);
				if (new SystemTypeManageForm(listPipingSystemType, listMechanicalSystemType, document, application, type).ShowDialog() == DialogResult.OK)
				{
					transaction.Commit();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)(int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		public List<PipingSystemType> Get_PipingSystemType(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(PipingSystemType)).ToElements();
			List<PipingSystemType> list = new List<PipingSystemType>();
			foreach (Element element in enumerable)
			{
				PipingSystemType item = element as PipingSystemType;
				list.Add(item);
			}
			return list;
		}

		public List<MechanicalSystemType> Get_MechanicalSystemType(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(MechanicalSystemType)).ToElements();
			List<MechanicalSystemType> list = new List<MechanicalSystemType>();
			foreach (Element element in enumerable)
			{
				MechanicalSystemType item = element as MechanicalSystemType;
				list.Add(item);
			}
			return list;
		}
	}
}
